﻿#region Usings

using System;
using System.IO;
using InfoService.BackgroundWorkers;
using InfoService.Weather;
using MediaPortal.Configuration;
using MediaPortal.GUI.Library;
using MediaPortal.Profile;
using WeatherConnector.Data;

#endregion

namespace InfoService.Utils
{
    public static class WeatherUtils
    {
        private static readonly Logger logger = Logger.GetInstance();
        public static void UpdateWeatherData()
        {
            using (Settings settings = new Settings(Config.GetFile(Config.Dir.Config, "MediaPortal.xml")))
            {
                string newId = settings.GetValueAsString("weather", "location", string.Empty);
                if (WeatherService.CurrentLocationId != newId)
                {
                    if (!string.IsNullOrEmpty(newId))
                    {
                        WeatherService.CurrentLocationId = newId;
                        logger.WriteLog("Location ID has changed to " + newId, LogLevel.Debug, InfoServiceModul.Weather);
                    }
                    else
                    {
                        logger.WriteLog("New Location ID was empty. Using the old Location ID -> " + WeatherService.CurrentLocationId, LogLevel.Debug, InfoServiceModul.Weather);
                    }
                }
            }
            WeatherWorker wWorker = new WeatherWorker(WeatherService.CurrentLocationId);
            wWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(wWorker_RunWorkerCompleted);
            wWorker.RunWorkerAsync();

        }

        static void wWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            if(!e.Cancelled && (bool)e.Result)
            {
                InfoServiceUtils.SetProperty("#infoservice.weather.location", WeatherService.Weather.Location);
                InfoServiceUtils.SetProperty("#infoservice.weather.today.temp", WeatherService.Weather.WeatherData.TodayTemperature);
                InfoServiceUtils.SetProperty("#infoservice.weather.today.maxtemp", WeatherService.Weather.WeatherData.TodayMaxTemperature);
                InfoServiceUtils.SetProperty("#infoservice.weather.today.mintemp", WeatherService.Weather.WeatherData.TodayMinTemperature);
                InfoServiceUtils.SetProperty("#infoservice.weather.today.feelsliketemp", WeatherService.Weather.WeatherData.TodayFeelsLikeTemperature);
                InfoServiceUtils.SetProperty("#infoservice.weather.today.humidity", WeatherService.Weather.WeatherData.TodayHumidity);
                InfoServiceUtils.SetProperty("#infoservice.weather.today.sunrise", WeatherService.Weather.WeatherData.TodaySunrise.ToShortTimeString());
                InfoServiceUtils.SetProperty("#infoservice.weather.today.sunset", WeatherService.Weather.WeatherData.TodaySunset.ToShortTimeString());
                InfoServiceUtils.SetProperty("#infoservice.weather.today.uvindex", WeatherService.Weather.WeatherData.TodayUvIndex);
                InfoServiceUtils.SetProperty("#infoservice.weather.today.wind",
                                               string.Format(GUILocalizeStrings.Get(555),
                                                             WeatherService.Weather.WeatherData.TodayWindDirection,
                                                             WeatherService.Weather.WeatherData.TodayWindSpeed,
                                                             WeatherService.Weather.WeatherEntities.WindSpeed));
                InfoServiceUtils.SetProperty("#infoservice.weather.today.condition", WeatherService.Weather.WeatherData.TodayCondition);
                InfoServiceUtils.SetProperty("#infoservice.weather.today.img.small.fullpath", Config.GetFile(Config.Dir.Weather, string.Format(@"64x64\{0}.png", WeatherService.Weather.WeatherData.TodayImageIcon)));
                InfoServiceUtils.SetProperty("#infoservice.weather.today.img.small.filenamewithext", Path.GetFileName(Config.GetFile(Config.Dir.Weather, string.Format(@"64x64\{0}.png", WeatherService.Weather.WeatherData.TodayImageIcon))));
                InfoServiceUtils.SetProperty("#infoservice.weather.today.img.small.filenamewithoutext", Path.GetFileNameWithoutExtension(Config.GetFile(Config.Dir.Weather, string.Format(@"64x64\{0}.png", WeatherService.Weather.WeatherData.TodayImageIcon))));
                InfoServiceUtils.SetProperty("#infoservice.weather.today.img.big.fullpath", Config.GetFile(Config.Dir.Weather, string.Format(@"128x128\{0}.png", WeatherService.Weather.WeatherData.TodayImageIcon)));
                InfoServiceUtils.SetProperty("#infoservice.weather.today.img.big.filenamewithext", Path.GetFileName(Config.GetFile(Config.Dir.Weather, string.Format(@"128x128\{0}.png", WeatherService.Weather.WeatherData.TodayImageIcon))));
                InfoServiceUtils.SetProperty("#infoservice.weather.today.img.big.filenamewithoutext", Path.GetFileNameWithoutExtension(Config.GetFile(Config.Dir.Weather, string.Format(@"128x128\{0}.png", WeatherService.Weather.WeatherData.TodayImageIcon))));
                InfoServiceUtils.SetProperty("#infoservice.weather.today.weekday", GetLocalizedDate(WeatherService.Weather.WeatherData.TodayDate));
                InfoServiceUtils.SetProperty("#infoservice.weather.lastupdated.message", string.Format(InfoServiceUtils.GetLocalizedLabel(31), WeatherService.LastRefresh));
                InfoServiceUtils.SetProperty("#infoservice.weather.lastupdated.datetime", WeatherService.LastRefresh.ToString());

                int daynum = 2;
                foreach (WeatherForecast wfc in WeatherService.Weather.WeatherData.WeatherForecast)
                {
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".mintemp", wfc.MinTemperature);
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".maxtemp", wfc.MaxTemperature);
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".sunrise", wfc.Sunrise.ToShortTimeString());
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".sunset", wfc.Sunset.ToShortTimeString());
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".day.condition", wfc.Day.Condition); // GUILocalizeStrings.Get(0x17a) + ": " + wfc.Day.Condition
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".night.condition", wfc.Night.Condition);
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".day.wind",
                                                   string.Format(GUILocalizeStrings.Get(555),
                                                                 wfc.Day.WindDirection,
                                                                 wfc.Day.WindSpeed,
                                                                 WeatherService.Weather.WeatherEntities.WindSpeed));
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".night.wind",
                                                   string.Format(GUILocalizeStrings.Get(555),
                                                                 wfc.Night.WindDirection,
                                                                 wfc.Night.WindSpeed,
                                                                 WeatherService.Weather.WeatherEntities.WindSpeed));
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".day.humidity", wfc.Day.Humidity);
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".night.humidity", wfc.Night.Humidity);
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".day.img.small.fullpath", Config.GetFile(Config.Dir.Weather, string.Format(@"64x64\{0}.png", wfc.Day.WeatherImage)));
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".day.img.small.filenamewithext", Path.GetFileName(Config.GetFile(Config.Dir.Weather, string.Format(@"64x64\{0}.png", wfc.Day.WeatherImage))));
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".day.img.small.filenamewithoutext", Path.GetFileNameWithoutExtension(Config.GetFile(Config.Dir.Weather, string.Format(@"64x64\{0}.png", wfc.Day.WeatherImage))));
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".day.img.big.fullpath", Config.GetFile(Config.Dir.Weather, string.Format(@"128x128\{0}.png", wfc.Day.WeatherImage)));
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".day.img.big.filenamewithext", Path.GetFileName(Config.GetFile(Config.Dir.Weather, string.Format(@"128x128\{0}.png", wfc.Day.WeatherImage))));
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".day.img.big.filenamewithoutext", Path.GetFileNameWithoutExtension(Config.GetFile(Config.Dir.Weather, string.Format(@"128x128\{0}.png", wfc.Day.WeatherImage))));
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".night.img.small.fullpath", Config.GetFile(Config.Dir.Weather, string.Format(@"64x64\{0}.png", wfc.Night.WeatherImage)));
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".night.img.small.filenamewithext", Path.GetFileName(Config.GetFile(Config.Dir.Weather, string.Format(@"64x64\{0}.png", wfc.Night.WeatherImage))));
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".night.img.small.filenamewithoutext", Path.GetFileNameWithoutExtension(Config.GetFile(Config.Dir.Weather, string.Format(@"64x64\{0}.png", wfc.Night.WeatherImage))));
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".night.img.big.fullpath", Config.GetFile(Config.Dir.Weather, string.Format(@"128x128\{0}.png", wfc.Night.WeatherImage)));
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".night.img.big.filenamewithext", Path.GetFileName(Config.GetFile(Config.Dir.Weather, string.Format(@"128x128\{0}.png", wfc.Night.WeatherImage))));
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".night.img.big.filenamewithoutext", Path.GetFileNameWithoutExtension(Config.GetFile(Config.Dir.Weather, string.Format(@"128x128\{0}.png", wfc.Night.WeatherImage))));
                    InfoServiceUtils.SetProperty("#infoservice.weather.day" + daynum + ".weekday", GetLocalizedDate(wfc.Date));
                    daynum++;
                }
            }
        }

        private static string GetLocalizedDate(DateTime date)
        {
            switch (date.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    return GUILocalizeStrings.Get(11);
                case DayOfWeek.Tuesday:
                    return GUILocalizeStrings.Get(12);
                case DayOfWeek.Wednesday:
                    return GUILocalizeStrings.Get(13);
                case DayOfWeek.Thursday:
                    return GUILocalizeStrings.Get(14);
                case DayOfWeek.Friday:
                    return GUILocalizeStrings.Get(15);
                case DayOfWeek.Saturday:
                    return GUILocalizeStrings.Get(16);
                case DayOfWeek.Sunday:
                    return GUILocalizeStrings.Get(17);
                default:
                    return "";
            }
        }
    }
}
